﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentNHibernate.Mapping;
using YasharEl.Infrastructure.ProcessEngine.EntityModel;
using YasharEl.Infrastructure.ProcessEngine.EntityModel.Prototype;
using YasharEl.Infrastructure.ProcessEngine.Prototype;

namespace YasharEl.Infrastructure.ProcessEngine.PersistenceModel.NHibernate.Mappings
{
    public class ProcessDefinitionEntityMap : ClassMap<ProcessDefinitionEntity>
    {
        public ProcessDefinitionEntityMap()
        {
            ReadOnly();
            Cache.ReadOnly();

            Table("ProcessDefinitions");

            Id(e => e.ProcessDefinitionId)
                .Column("ProcessDefinitionId")
                    .GeneratedBy.Assigned();

            Map(e => e.Name)
                .Column("ProcessDefinitionName")
                .Length(250)
                .Not.Nullable();
            Map(e => e.Description)
                .Column("ProcessDefinitionDescription")
                .Length(1000)
                .Not.Nullable();
            Map(e => e.SLA)
                .Column("SLA")
                .Not.Nullable();
            Map(e => e.WorkflowXaml)
                .Column("WorkflowXaml")
                .Length(255)
                .Not.Nullable();

            References(e => e.BelongsToGroup)
                .Column("ProcessDefinitionGroupId")
                .Class<ProcessDefinitionGroupEntity>()
                .LazyLoad();

            HasMany<ProcessDefinitionPropertyMappingEntity>(e => e.DefinedProperties)
                .Inverse()
                .LazyLoad()
                .Cascade.All()
                .KeyColumn("PrototypedObjectId")
                .Where
                (
                    "PrototypedObjectType = " + ProcessDefinitionObjectType.ProcessDefinition.Value.ToString()
                );
        }
    }
}
